﻿namespace IPTableAdapters
{
    using System;
    using System.CodeDom.Compiler;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics;

    [ToolboxItem(true), DesignerCategory("code"), HelpKeyword("vs.data.TableAdapter"), GeneratedCode("System.Data.Design.TypedDataSetGenerator", "2.0.0.0"), DataObject(true), Designer("Microsoft.VSDesigner.DataSource.Design.TableAdapterDesigner, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    public class HeadingSetTemplateTableAdapter : Component
    {
        private SqlDataAdapter _adapter;
        private bool _clearBeforeFill;
        private SqlCommand[] _commandCollection;
        private SqlConnection _connection;

        /*[DebuggerNonUserCode]*/
        public HeadingSetTemplateTableAdapter()
        {
            this.ClearBeforeFill = true;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true), DebuggerNonUserCode, HelpKeyword("vs.data.TableAdapter")]
        public virtual int Delete(string Original_HeadingSet, string Original_HeadingCode, int Original_Section)
        {
            int ret;
            if (Original_HeadingSet == null)
            {
                throw new ArgumentNullException("Original_HeadingSet");
            }
            this.Adapter.DeleteCommand.Parameters[0].Value = Original_HeadingSet;
            if (Original_HeadingCode == null)
            {
                throw new ArgumentNullException("Original_HeadingCode");
            }
            this.Adapter.DeleteCommand.Parameters[1].Value = Original_HeadingCode;
            this.Adapter.DeleteCommand.Parameters[2].Value = Original_Section;
            ConnectionState previousConnectionState = this.Adapter.DeleteCommand.Connection.State;
            if ((this.Adapter.DeleteCommand.Connection.State & ConnectionState.Open) != ConnectionState.Open)
            {
                this.Adapter.DeleteCommand.Connection.Open();
            }
            try
            {
                ret = this.Adapter.DeleteCommand.ExecuteNonQuery();
            }
            finally
            {
                if (previousConnectionState == ConnectionState.Closed)
                {
                    this.Adapter.DeleteCommand.Connection.Close();
                }
            }
            return ret;
        }

        [HelpKeyword("vs.data.TableAdapter"), DebuggerNonUserCode, DataObjectMethod(DataObjectMethodType.Select, true)]
        public virtual IP.HeadingSetTemplateDataTable GetHeadingSetTemplateData()
        {
            this.Adapter.SelectCommand = this.CommandCollection[0];
            IP.HeadingSetTemplateDataTable dataTable = new IP.HeadingSetTemplateDataTable();
            this.Adapter.Fill(dataTable);
            return dataTable;
        }

        [HelpKeyword("vs.data.TableAdapter"), DataObjectMethod(DataObjectMethodType.Select, true)]
        public DataTable GetHeadingSetTemplateDatabyljr(string HeadingSet, string HeadingCode)
        {
            /*
             SELECT HeadingSet, HeadingCode, '' AS HeadingName, HeadingSetTemplate.Section, hs.Description as SectionName, CreateBy, CreateDate, UpdateBy, UpdateDate, HeadingSetTemplate.OwnerGroupID FROM HeadingSetTemplate, headingSection hs WHERE (1 = 1)
                and HeadingSetTemplate.Section = hs.Section
             */
            SqlCommand sql = new SqlCommand();
            //sql.CommandText = "SELECT HeadingSet, HeadingCode, '' AS HeadingName, hst.Section, hs.Description as SectionName, hst.CreateBy, hst.CreateDate, hst.UpdateBy, hst.UpdateDate, hst.OwnerGroupID FROM HeadingSetTemplate hst, headingSection hs WHERE (1 = 1) and hst.Section = hs.Section";
            sql.CommandText = @"SELECT hst.HeadingSet, hst.HeadingCode, ht.HeadingName AS HeadingName, hst.Section, 
                                    hs.Description as SectionName, hst.CreateBy, hst.CreateDate, hst.UpdateBy, 
                                    hst.UpdateDate, hst.OwnerGroupID 
                                    FROM HeadingSetTemplate hst, headingSection hs , headingtemplate ht
                                    WHERE (1 = 1) and hst.Section = hs.Section and hst.HeadingCode = ht.HeadingCode";

            if (HeadingCode != null && HeadingCode != "")
            {
                sql.CommandText += " and  HeadingSet like '%" + HeadingSet + "%'";
            }
            if (HeadingCode != null && HeadingCode != "")
            {
                sql.CommandText += " and  HeadingCode like '%" + HeadingCode + "%'";
            }
            sql.CommandType = CommandType.Text;
            sql.Connection = this.Connection;
            this.Adapter.SelectCommand = sql;
            DataTable dataTable = new DataTable();
            this.Adapter.Fill(dataTable);
            return dataTable;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false), DebuggerNonUserCode, HelpKeyword("vs.data.TableAdapter")]
        public virtual IP.HeadingSetTemplateDataTable GetHeadingSetTemplateDataByHeadingSet(string HeadingSet)
        {
            this.Adapter.SelectCommand = this.CommandCollection[1];
            if (HeadingSet == null)
            {
                throw new ArgumentNullException("HeadingSet");
            }
            this.Adapter.SelectCommand.Parameters[0].Value = HeadingSet;
            IP.HeadingSetTemplateDataTable dataTable = new IP.HeadingSetTemplateDataTable();
            this.Adapter.Fill(dataTable);
            return dataTable;
        }

        /*[DebuggerNonUserCode]*/
        private void InitAdapter()
        {
            this._adapter = new SqlDataAdapter();
            DataTableMapping tableMapping = new DataTableMapping();
            tableMapping.SourceTable = "Table";
            tableMapping.DataSetTable = "HeadingSetTemplate";
            tableMapping.ColumnMappings.Add("HeadingSet", "HeadingSet");
            tableMapping.ColumnMappings.Add("HeadingCode", "HeadingCode");
            tableMapping.ColumnMappings.Add("Section", "Section");
            tableMapping.ColumnMappings.Add("CreateBy", "CreateBy");
            tableMapping.ColumnMappings.Add("UpdateBy", "UpdateBy");
            tableMapping.ColumnMappings.Add("OwnerGroupID", "OwnerGroupID");
            tableMapping.ColumnMappings.Add("CreateDate", "CreateDate");
            tableMapping.ColumnMappings.Add("UpdateDate", "UpdateDate");
            tableMapping.ColumnMappings.Add("HeadingName", "HeadingName");
            this._adapter.TableMappings.Add(tableMapping);
            this._adapter.DeleteCommand = new SqlCommand();
            this._adapter.DeleteCommand.Connection = this.Connection;
            this._adapter.DeleteCommand.CommandText = "DELETE FROM [dbo].[HeadingSetTemplate] WHERE (([HeadingSet] = @Original_HeadingSet) AND ([HeadingCode] = @Original_HeadingCode) AND ([Section] = @Original_Section))";
            this._adapter.DeleteCommand.CommandType = CommandType.Text;
            this._adapter.DeleteCommand.Parameters.Add(new SqlParameter("@Original_HeadingSet", SqlDbType.NVarChar, 0, ParameterDirection.Input, 0, 0, "HeadingSet", DataRowVersion.Original, false, null, "", "", ""));
            this._adapter.DeleteCommand.Parameters.Add(new SqlParameter("@Original_HeadingCode", SqlDbType.NVarChar, 0, ParameterDirection.Input, 0, 0, "HeadingCode", DataRowVersion.Original, false, null, "", "", ""));
            this._adapter.DeleteCommand.Parameters.Add(new SqlParameter("@Original_Section", SqlDbType.Int, 0, ParameterDirection.Input, 0, 0, "Section", DataRowVersion.Original, false, null, "", "", ""));
            this._adapter.InsertCommand = new SqlCommand();
            this._adapter.InsertCommand.Connection = this.Connection;
            this._adapter.InsertCommand.CommandText = "INSERT INTO HeadingSetTemplate\r\n                      (HeadingSet, HeadingCode, Section, CreateBy, UpdateBy, OwnerGroupID)\r\nVALUES     (@HeadingSet,@HeadingCode,@Section,@CreateBy,@UpdateBy,@OwnerGroupID)";
            this._adapter.InsertCommand.CommandType = CommandType.Text;
            this._adapter.InsertCommand.Parameters.Add(new SqlParameter("@HeadingSet", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "HeadingSet", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.InsertCommand.Parameters.Add(new SqlParameter("@HeadingCode", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "HeadingCode", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.InsertCommand.Parameters.Add(new SqlParameter("@Section", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "Section", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.InsertCommand.Parameters.Add(new SqlParameter("@CreateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "CreateBy", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.InsertCommand.Parameters.Add(new SqlParameter("@UpdateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "UpdateBy", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.InsertCommand.Parameters.Add(new SqlParameter("@OwnerGroupID", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "OwnerGroupID", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.UpdateCommand = new SqlCommand();
            this._adapter.UpdateCommand.Connection = this.Connection;
            this._adapter.UpdateCommand.CommandText = "UPDATE    HeadingSetTemplate\r\nSET              Section = @Section, UpdateBy = @UpdateBy, UpdateDate = GETDATE(), OwnerGroupID = @OwnerGroupID\r\nWHERE     (HeadingSet = @Original_HeadingSet) AND (HeadingCode = @Original_HeadingCode) AND (Section = @Original_Section)";
            this._adapter.UpdateCommand.CommandType = CommandType.Text;
            this._adapter.UpdateCommand.Parameters.Add(new SqlParameter("@Section", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "Section", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.UpdateCommand.Parameters.Add(new SqlParameter("@UpdateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "UpdateBy", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.UpdateCommand.Parameters.Add(new SqlParameter("@OwnerGroupID", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "OwnerGroupID", DataRowVersion.Current, false, null, "", "", ""));
            this._adapter.UpdateCommand.Parameters.Add(new SqlParameter("@Original_HeadingSet", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "HeadingSet", DataRowVersion.Original, false, null, "", "", ""));
            this._adapter.UpdateCommand.Parameters.Add(new SqlParameter("@Original_HeadingCode", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "HeadingCode", DataRowVersion.Original, false, null, "", "", ""));
            this._adapter.UpdateCommand.Parameters.Add(new SqlParameter("@Original_Section", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "Section", DataRowVersion.Original, false, null, "", "", ""));
        }

        /*[DebuggerNonUserCode]*/
        private void InitCommandCollection()
        {
            this._commandCollection = new SqlCommand[2];
            this._commandCollection[0] = new SqlCommand();
            this._commandCollection[0].Connection = this.Connection;
            this._commandCollection[0].CommandText = "SELECT     HeadingSet, HeadingCode, NULL AS HeadingName, Section, CreateBy, CreateDate, UpdateBy, UpdateDate, OwnerGroupID\r\nFROM         HeadingSetTemplate\r\nWHERE     (1 = 2)";
            this._commandCollection[0].CommandType = CommandType.Text;
            this._commandCollection[1] = new SqlCommand();
            this._commandCollection[1].Connection = this.Connection;
            this._commandCollection[1].CommandText = "SELECT     HeadingSetTemplate.HeadingSet, HeadingSetTemplate.HeadingCode, HeadingSetTemplate.Section, HeadingSetTemplate.CreateBy, \r\n                      HeadingSetTemplate.UpdateBy, HeadingSetTemplate.OwnerGroupID, HeadingTemplate.HeadingName\r\nFROM         HeadingSetTemplate LEFT OUTER JOIN\r\n                      HeadingTemplate ON HeadingSetTemplate.HeadingCode = HeadingTemplate.HeadingCode AND \r\n                      HeadingSetTemplate.Section = HeadingTemplate.Section\r\nWHERE     (HeadingSetTemplate.HeadingSet = @HeadingSet)";
            this._commandCollection[1].CommandType = CommandType.Text;
            this._commandCollection[1].Parameters.Add(new SqlParameter("@HeadingSet", SqlDbType.NVarChar, 60, ParameterDirection.Input, 0, 0, "HeadingSet", DataRowVersion.Current, false, null, "", "", ""));
        }

        /*[DebuggerNonUserCode]*/
        private void InitConnection()
        {
            this._connection = new SqlConnection();
            this._connection.ConnectionString = ConfigurationManager.ConnectionStrings["LocalSqlConnection1"].ConnectionString;
        }

        [HelpKeyword("vs.data.TableAdapter"), DataObjectMethod(DataObjectMethodType.Insert, true), DebuggerNonUserCode]
        public virtual int Insert(string HeadingSet, string HeadingCode, int Section, string CreateBy, string UpdateBy, string OwnerGroupID)
        {
            int ret;
            if (HeadingSet == null)
            {
                throw new ArgumentNullException("HeadingSet");
            }
            this.Adapter.InsertCommand.Parameters[0].Value = HeadingSet;
            if (HeadingCode == null)
            {
                throw new ArgumentNullException("HeadingCode");
            }
            this.Adapter.InsertCommand.Parameters[1].Value = HeadingCode;
            this.Adapter.InsertCommand.Parameters[2].Value = Section;
            if (CreateBy == null)
            {
                throw new ArgumentNullException("CreateBy");
            }
            this.Adapter.InsertCommand.Parameters[3].Value = CreateBy;
            if (UpdateBy == null)
            {
                throw new ArgumentNullException("UpdateBy");
            }
            this.Adapter.InsertCommand.Parameters[4].Value = UpdateBy;
            if (OwnerGroupID == null)
            {
                throw new ArgumentNullException("OwnerGroupID");
            }
            this.Adapter.InsertCommand.Parameters[5].Value = OwnerGroupID;
            ConnectionState previousConnectionState = this.Adapter.InsertCommand.Connection.State;
            if ((this.Adapter.InsertCommand.Connection.State & ConnectionState.Open) != ConnectionState.Open)
            {
                this.Adapter.InsertCommand.Connection.Open();
            }
            try
            {
                ret = this.Adapter.InsertCommand.ExecuteNonQuery();
            }
            finally
            {
                if (previousConnectionState == ConnectionState.Closed)
                {
                    this.Adapter.InsertCommand.Connection.Close();
                }
            }
            return ret;
        }

        [HelpKeyword("vs.data.TableAdapter"), DebuggerNonUserCode]
        public virtual int Update(IP dataSet)
        {
            return this.Adapter.Update(dataSet, "HeadingSetTemplate");
        }

        [HelpKeyword("vs.data.TableAdapter"), DebuggerNonUserCode]
        public virtual int Update(DataRow[] dataRows)
        {
            return this.Adapter.Update(dataRows);
        }

        [HelpKeyword("vs.data.TableAdapter"), DebuggerNonUserCode]
        public virtual int Update(IP.HeadingSetTemplateDataTable dataTable)
        {
            return this.Adapter.Update(dataTable);
        }

        [DebuggerNonUserCode, HelpKeyword("vs.data.TableAdapter")]
        public virtual int Update(DataRow dataRow)
        {
            return this.Adapter.Update(new DataRow[] { dataRow });
        }

        [HelpKeyword("vs.data.TableAdapter"), DataObjectMethod(DataObjectMethodType.Update, true)]
        public virtual int Update(int Section, string UpdateBy, string OwnerGroupID, string Original_HeadingSet, string Original_HeadingCode, int Original_Section)
        {
            int ret;
            this.Adapter.UpdateCommand.Parameters[0].Value = Section;
            if (UpdateBy == null)
            {
                throw new ArgumentNullException("UpdateBy");
            }
            this.Adapter.UpdateCommand.Parameters[1].Value = UpdateBy;
            if (OwnerGroupID == null)
            {
                throw new ArgumentNullException("OwnerGroupID");
            }
            this.Adapter.UpdateCommand.Parameters[2].Value = OwnerGroupID;
            if (Original_HeadingSet == null)
            {
                throw new ArgumentNullException("Original_HeadingSet");
            }
            this.Adapter.UpdateCommand.Parameters[3].Value = Original_HeadingSet;
            if (Original_HeadingCode == null)
            {
                throw new ArgumentNullException("Original_HeadingCode");
            }
            this.Adapter.UpdateCommand.Parameters[4].Value = Original_HeadingCode;
            this.Adapter.UpdateCommand.Parameters[5].Value = Original_Section;
            ConnectionState previousConnectionState = this.Adapter.UpdateCommand.Connection.State;
            if ((this.Adapter.UpdateCommand.Connection.State & ConnectionState.Open) != ConnectionState.Open)
            {
                this.Adapter.UpdateCommand.Connection.Open();
            }
            try
            {
                ret = this.Adapter.UpdateCommand.ExecuteNonQuery();
            }
            finally
            {
                if (previousConnectionState == ConnectionState.Closed)
                {
                    this.Adapter.UpdateCommand.Connection.Close();
                }
            }
            return ret;
        }

        /*[DebuggerNonUserCode]*/
        private SqlDataAdapter Adapter
        {
            get
            {
                if (this._adapter == null)
                {
                    this.InitAdapter();
                }
                return this._adapter;
            }
        }

        /*[DebuggerNonUserCode]*/
        public bool ClearBeforeFill
        {
            get
            {
                return this._clearBeforeFill;
            }
            set
            {
                this._clearBeforeFill = value;
            }
        }

        /*[DebuggerNonUserCode]*/
        protected SqlCommand[] CommandCollection
        {
            get
            {
                if (this._commandCollection == null)
                {
                    this.InitCommandCollection();
                }
                return this._commandCollection;
            }
        }

        /*[DebuggerNonUserCode]*/
        internal SqlConnection Connection
        {
            get
            {
                if (this._connection == null)
                {
                    this.InitConnection();
                }
                return this._connection;
            }
            set
            {
                this._connection = value;
                if (this.Adapter.InsertCommand != null)
                {
                    this.Adapter.InsertCommand.Connection = value;
                }
                if (this.Adapter.DeleteCommand != null)
                {
                    this.Adapter.DeleteCommand.Connection = value;
                }
                if (this.Adapter.UpdateCommand != null)
                {
                    this.Adapter.UpdateCommand.Connection = value;
                }
                for (int i = 0; i < this.CommandCollection.Length; i++)
                {
                    if (this.CommandCollection[i] != null)
                    {
                        this.CommandCollection[i].Connection = value;
                    }
                }
            }
        }
    }
}

